package com.zhengxin.one.common;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class ErrorHandler {

    @ExceptionHandler(ApiException.class)
    protected ResponseEntity<Map> handleApiException(ApiException ex) {
        Map<String,Object> res = new HashMap<>();
        res.put("stats",ex.getStats());
        res.put("msg",ex.getMsg());
        return  new ResponseEntity<>(res, HttpStatus.valueOf(ex.getStats()));
    }
}
